#include <stdio.h>

#include<pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void* thread_main(void * arg);

void* thread_inc(void * arg);
void* thread_des(void * arg);
long long num =0;

#define NUM_THREAD 100
pthread_mutex_t mutex;

int main(int argc,char * argv[])
{
    pthread_t tid[NUM_THREAD];
    //void* thr_ret;

    int thread_param =5;
    for(int i=0;i<NUM_THREAD;i++)
    {
        if(i%2)
            pthread_create(&(tid[i]),NULL,thread_inc,NULL);
        else
            pthread_create(&(tid[i]),NULL,thread_des,NULL);
    }
    for(int i=0;i<NUM_THREAD;i++)
    {
        pthread_join(tid[i],NULL);
    }

    
    printf("num :%lld \n",num);
    return 0;
}

// void* thread_main(void * arg)
// {

//     int cnt = *(int *) arg;
//     char * msg = (char*) malloc(50*sizeof(char));
//     strcpy(msg,"hello, I am thread \n");

//     for(int i=0;i<cnt;i++)
//     {
//         sleep(1);
//         puts("running thread");
//     }
//     return (void*) msg;
// } 

void* thread_inc(void * arg)
{
    for(int i=0;i<5000000;i++)
    {
        pthread_mutex_lock(&mutex);
        num +=1;
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
void* thread_des(void * arg)
{
    for(int i=0;i<5000000;i++)
    {
        pthread_mutex_lock(&mutex);
        num -=1;
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}